<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class UserService extends BaseService
{
    //用户

    /**
     * 用户列表
     */
    public function getUlist($pas)
    {
        $db = $this->dbConn($this->dbUser);
        if ($pas['nick']) {
            $db = $db->where('nickname', 'like', '%'.$pas['nick'].'%');
        }
        if ($pas['phone']) {
            $db = $db->where('phone', 'like', '%'.$pas['phone'].'%');
        }
        $res = $db->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有用户');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $head = file_exists($v['avatarUrl']) ? $v['avatarUrl'] : '';
            $levelOne = (array) $this->dbConn($this->dbUlevel)->where('id', $v['level_id'])->first();
            $readNum = $this->dbConn($this->dbUtrace)->where('uid', $v['id'])->count();
            $addressNum = $this->dbConn($this->dbUaddress)->where('uid', $v['id'])->count();
            $couponNum = $this->dbConn($this->dbUcoupon)->where('uid', $v['id'])->count();
            $payNum = $this->dbConn($this->dbPay)
                ->where('uid', $v['id'])
                ->where('type', 1)
                ->where('status', 2)
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'phone' => $v['phone'] ? $v['phone'] : '-',
                'nickname' => $v['nickname'],
                'prefix' => env('DOMAIN') . '/',
                'avatarUrl' => $head,
                'lasttime' => $v['lasttime'],
                'balance' => $v['balance'] / 100,
                'levelName' => empty($levelOne) ? '-' : $levelOne['title'],
                'discount' => $v['discount'],
                'readNum' => $readNum,
                'addressNum' => $addressNum,
                'couponNum' => $couponNum,
                'rechargeNum' => $payNum,
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }
}